<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 基础设置
 */
namespace app\admin\controller;

use  Mscore\Core\Controller;
use  app\admin\model\Basicset as BasicsetModel;
use  app\admin\model\Common as CommonModel;
use  Mscore\Core\Lib\Upload;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class BasicsetController extends Controller{

	private $model;
    private $login;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new BasicsetModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    
    /**
     * 基础信息设置
     * @param array $set 
     * @param array $where 
     */	
    public function set(){
        $params = post();
        $authtype=(new CommonModel())->getBaseinfo('keyname="authweight"', 'content')['content'];
        if(!isset($authtype['authtype']) || empty($authtype['authtype'])){$authtype['authtype']=1;}//安全提交配置
        if($params){
            unset($params['authtype']);
            if($authtype['authtype']>1){
                $id=(new CommonModel())->getcode($params['code']);
                if(!$id){return $this->echojson(Lang::get('Authcode'));}else{ unset($params['code']);}
            }
            $status = $this->model->save($params);
            switch ($status) {
                case true:
                    logrecord('配置信息修改添加成功', $this->login['info']);
                    $this->echoJson($this->success);
                    break;
                default:
                    logrecord('配置信息修改添加失败', $this->login['info']);
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            $datainfo=[];
            if(get('id')){
               $datainfo = $this->model->basicset(get('id'));
            }
            //定义变量datainfo输出
            $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
            $datainfo['name']=isset($datainfo['name'])?$datainfo['name']:'';
            $datainfo['keyname']=isset($datainfo['keyname'])?$datainfo['keyname']:'';
            $datainfo['type']=isset($datainfo['type'])?$datainfo['type']:0;
            $datainfo['content']=isset($datainfo['content'])?$datainfo['content']:'';
            $datainfo['notes']=isset($datainfo['notes'])?$datainfo['notes']:'';
            $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;

            $this->assign('authtype',$authtype);
            $this->assign('userinfo', $this->login['info']['grade']);
            $this->assign('datainfo', $datainfo);
            switch ($datainfo['type']) {
                case 1:
                    $this->display('basicset.html');
                    break;
                default:
                    $this->display('set.html');
                    break;
            }    
        }   
    }

    /**
     * 产品发布
     * @param array $release
     * @param array $where
     */
    public function release()
    {
        if(post('id')){
            $info = $this->model->release(post('id'));
        }
        $file=base_path().'/config/'.$info['keyname'].'.php';
        //缓存
        $info=$info['content'];
        $text='<?php  return '.var_export($info,true).';';
        if(false!==fopen($file,'w+')){  
            file_put_contents($file,$text);
            logrecord($info['keyname'].'配置信息发布成功', $this->login['info']);
            $this->echoJson($this->success);
        }else{  
            logrecord($info['keyname'].'配置信息发布失败', $this->login['info']);
            $this->echoJson($this->error);
        }
    }

    /**
     * 基础信息设置
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' dels<1 ';
                if($this->login['info']['grade']!="superadmin"){
                    $where .= ' and status > 1 ';
                }
                $datalist = $this->model->list($where,$page,$limit);
                $data =[
                    "code" => 200,
                    "data" => $datalist['datalist'],
                    "count"=> $datalist['total'],
                    "msg" => 'ok'
                ];
                $this->echojson($data);
                break;
            default:
                $this->display('list.html');
                break;
        }
    }

    /**
     * 基础信息保存
     * @param array $basicssave 
     * @param array $where 
     */ 
    public function basicsave(){
        if(post('id')){
            $baimg = (new CommonModel)->getbasics('id='.post('id'),'content');
        }
        $baimg=json_decode($baimg['content'],true);
        switch (true) {
            case isset($_POST['imgOne1'])&&!empty($_POST['imgOne1']):
                $url="/static/upload/site/";
                $site_logo = (new Upload)->Uploadbase64($_POST['imgOne1'],$url,'log'.time());
                if($baimg['site_logo']){
                   @unlink (configs('file.path').$baimg['site_logo']);   
                }
                break;
            default:
                $site_logo=$baimg['site_logo'];
                break;
        }
        switch (true) {
            case isset($_POST['imgOne2'])&&!empty($_POST['imgOne2']):
                $url="/static/upload/site/";
                $site_logoback = (new Upload)->Uploadbase64($_POST['imgOne2'],$url,'slo'+time());
                if($baimg['site_logoback']){
                   @unlink (configs('file.path').$baimg['site_logoback']);   
                }
                break;
            default:
                $site_logoback=$baimg['site_logoback'];
                break;
        }
        switch (true) {
            case isset($_POST['imgOne3'])&&!empty($_POST['imgOne3']):
                $url="/static/upload/site/";
                $site_favicon = (new Upload)->Uploadbase64($_POST['imgOne3'],$url,'sf'.time());
                if($baimg['site_favicon']){
                   @unlink (configs('file.path').$baimg['site_favicon']);   
                }
                break;
            default:
                $site_favicon=$baimg['site_favicon'];
                break;
        }
        switch (true) {
            case isset($_POST['imgOne4'])&&!empty($_POST['imgOne4']):
                $url="/static/upload/site/";
                $site_logobottom = (new Upload)->Uploadbase64($_POST['imgOne4'],$url,'sl'.time());
                if($baimg['site_logobottom']){
                   @unlink (configs('file.path').$baimg['site_logobottom']);   
                }
                break;
            default:
                $site_logobottom=$baimg['site_logobottom'];
                break;
        }
        switch (true) {
            case isset($_POST['imgOne5'])&&!empty($_POST['imgOne5']):
                $url="/static/upload/site/";
                $site_ewm = (new Upload)->Uploadbase64($_POST['imgOne5'],$url,'em'.time());
                if($baimg['site_ewm']){
                   @unlink (configs('file.path').$baimg['site_ewm']);   
                }
                break;
            default:
                $site_ewm=$baimg['site_ewm'];
                break;
        }
        switch (true) {
            case isset($_POST['imgOne6'])&&!empty($_POST['imgOne6']):
                $url="/static/upload/site/";
                $site_wxewm = (new Upload)->Uploadbase64($_POST['imgOne6'],$url,'wem'.time());
                if($baimg['site_wxewm']){
                   @unlink (configs('file.path').$baimg['site_wxewm']);   
                }
            break;
            default:
                $site_wxewm=$baimg['site_wxewm'];
                break;
        }
        switch (true) {
            case isset($_POST['site_status'])&&!empty($_POST['site_status']):
                $site_status=1;
                break;
            default:
                $site_status=0;
                break;
        }
        $data=[
            'id'=>post('id'),
            'site_status'=>$site_status,
            'site_domain'=>$_POST['site_domain'],
            'site_name'=>$_POST['site_name'],
            'site_logo'=>$site_logo,
            'site_logoback'=>$site_logoback,
            'site_favicon'=>$site_favicon,
            'site_title'=>$_POST['site_title'],
            'site_keywords'=>$_POST['site_keywords'],
            'site_description'=>$_POST['site_description'],
            'site_statis'=>$_POST['site_statis'],
            'site_logobottom'=>$site_logobottom,
            'site_phone'=>$_POST['site_phone'],
            'site_email'=>$_POST['site_email'],
            'site_add'=>$_POST['site_add'],
            'site_ewm'=>$site_ewm,
            'site_wxewm'=>$site_wxewm,
            'site_copy'=>$_POST['site_copy'],
            'status'=>$_POST['status']
        ];
        $status=$this->model->basicssave($data);
        switch ($status) {
            case true:
                logrecord('基础信息修改添加成功', $this->login['info']);
                $this->echoJson($this->success);
                break;
            default:
                logrecord('基础信息修改添加失败', $this->login['info']);
                $this->echoJson($this->error);
                break;
        }   
    }

    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */  
    public function del(){ 
        $data=['id'=>post('id')];
        $status = $this->model->del($data);
        switch ($status) {
            case true:
                logrecord('配置信息删除成功', $this->login['info']);
                $this->echoJson($this->success);
                break;
            default:
                logrecord('配置信息删除失败', $this->login['info']);
                $this->echoJson($this->error);
                break;
        } 
    }

    /**
     * 汇率设置
     * @param array $changeRate 
     * @param array $where 
     */ 
    public function changerate()
    {
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' a.dels < 1 ';
            if(post('name')){
            $where .= ' and a.name like "%'.post('name').'%"';
            }
            if(post('codes')){
            $where .= ' and a.codes like "%'.post('codes').'%"';
            }
            if(post('abbr')){
            $where .= ' and a.abbr like "%'.post('abbr').'%"';
            }
            $datalist = $this->model->changerate($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echojson($data);
        } else {
            $this->display('exchange_list.html');
        }
    }

    /**
     * 添加汇率
     * @param array rateadd
     * @param array $where
     */
    public function rateadd(){
        $params = post();
        $authtype=(new CommonModel())->getBaseinfo('keyname="authweight"', 'content')['content'];
        if(!isset($authtype['authtype']) || empty($authtype['authtype'])){$authtype['authtype']=1;}//安全提交配置
        if($params){
            unset($params['authtype']);
            if($authtype['authtype']>1){
                $id=(new CommonModel())->getcode($params['code']);
                if(!$id){return $this->echojson(Lang::get('Authcode'));}else{ unset($params['code']);}
            }
            $status = $this->model->ratesave($params);
            switch ($status) {
                case true:
                    logrecord('汇率修改添加成功', $this->login['info']);
                    $this->echoJson($this->success);
                    break;
                default:
                    logrecord('汇率修改添加失败', $this->login['info']);
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            $rateinfo=[];
            if(get('id')){
               $rate=array('id'=>get('id'));
               $rateinfo = $this->model->rateinfo($rate);
            }
            //定义变量rateinfo输出
            $rateinfo['id']=isset($rateinfo['id'])?$rateinfo['id']:'';
            $rateinfo['name']=isset($rateinfo['name'])?$rateinfo['name']:'';
            $rateinfo['rate']=isset($rateinfo['rate'])?$rateinfo['rate']:'';
            $rateinfo['codes']=isset($rateinfo['codes'])?$rateinfo['codes']:'';
            $rateinfo['abbr']=isset($rateinfo['abbr'])?$rateinfo['abbr']:'';
            $rateinfo['defualt']=isset($rateinfo['defualt'])?$rateinfo['defualt']:0;
            $rateinfo['status']=isset($rateinfo['status'])?$rateinfo['status']:1;
            $rateinfo['ranges']=isset($rateinfo['ranges'])?$rateinfo['ranges']:0;
            $rateinfo['type']=isset($rateinfo['type'])?$rateinfo['type']:1;

             $this->assign('authtype',$authtype);
            $this->assign('datainfo', $rateinfo);
            $this->display('exchange_add.html');
        }
    }

    /**
     * 汇率删除
     * @param array $userdel 
     * @param array $where 
     */  
    public function ratedel(){ 
        $data=['id'=>post('id')];
        $status = $this->model->ratedel($data);
        switch ($status) {
            case true:
                logrecord('汇率删除成功', $this->login['info']);
                $this->echoJson($this->success);
                break;
            default:
                logrecord('汇率删除失败', $this->login['info']);
                $this->echoJson($this->error);
                break;
        }
    }

    /**
     * 客服设置
     * @param array $custer 
     * @param array $where 
     */ 
    public function custer()
    {
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' a.dels < 1 ';
            if(post('name')){
                $where .= ' and a.name like "%'.post('name').'%"';
            }
            $datalist = $this->model->custerlist($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echojson($data);
        } else {
            $this->display('custer_list.html');
        }
    }

    /**
     * 添加客服
     * @param array custeradd
     * @param array $where
     */
    public function custeradd(){
        $params = post();
        $authtype=(new CommonModel())->getBaseinfo('keyname="authweight"', 'content')['content'];
        if(!isset($authtype['authtype']) || empty($authtype['authtype'])){$authtype['authtype']=1;}//安全提交配置
        if($params){
            unset($params['authtype']);
            if($authtype['authtype']>1){
                $id=(new CommonModel())->getcode($params['code']);
                if(!$id){return $this->echojson(Lang::get('Authcode'));}else{ unset($params['code']);}
            }
            $status = $this->model->custersave($params);
            switch ($status) {
                case true:
                    logrecord('客服帐号修改添加成功', $this->login['info']);
                    $this->echoJson($this->success);
                    break;
                default:
                    logrecord('客服帐号修改添加失败', $this->login['info']);
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            $datainfo=[];
            if(get('id')){
               $data=array('id'=>get('id'));
               $datainfo = $this->model->custerinfo($data);
            }
            //定义变量datainfo输出
            $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
            $datainfo['name']=isset($datainfo['name'])?$datainfo['name']:'';
            $datainfo['titles']=isset($datainfo['titles'])?$datainfo['titles']:array();
            $datainfo['type']=isset($datainfo['type'])?$datainfo['type']:1;
            $datainfo['kefu_url']=isset($datainfo['kefu_url'])?$datainfo['kefu_url']:'';
            $datainfo['method']=isset($datainfo['method'])?$datainfo['method']:1;
            $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
            $datainfo['aid']=isset($datainfo['aid'])?$datainfo['aid']:'';
            $datainfo['realname']=isset($datainfo['realname'])?$datainfo['realname']:'';

            $langs=(new CommonModel)->getLang();
            $this->assign('authtype',$authtype);
            $this->assign('langs',$langs);
            $this->assign('datainfo', $datainfo);
            $this->display('custer_add.html');
        }
    }
    
    /**
     * 客服删除
     * @param array $custerdel 
     * @param array $where 
     */  
    public function custerdel(){ 
        $data=['id'=>post('id')];
        $status = $this->model->custerdel($data);
        switch ($status) {
            case true:
                logrecord('客服帐号删除成功', $this->login['info']);
                $this->echoJson($this->success);
                break;
            default:
                logrecord('客服帐号删除失败', $this->login['info']);
                $this->echoJson($this->error);
                break;
        }
    }

}